﻿using System;
using System.IO;
using CefSharp;

namespace Citi.SecurityWebBrowser
{
    public class DownloadHandler : IDownloadHandler
    {
        private string path;
        private Stream stream;

        public DownloadHandler(string folderSave)
        {
            path = folderSave;
            //File.WriteAllText(string.Concat(path, ".tmpdwn"), DateTime.Now.ToString());
            
        }

        public bool ReceivedData(byte[] data)
        {
            stream.Write(data, 0, data.Length);
            return true;
            //stream.Write(data, 0, data.GetLength(0));
            //return true;

        }
        public void Complete()
        {
            stream.Dispose();
            stream = null;

            File.AppendAllText(string.Concat(path, ".tmpdwn"), DateTime.Now.ToString());
            Console.WriteLine("Downloaded: {0}", path);
        }


        public bool OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog)
        {
            //path = Path.Combine(path, suggestedName);
            //stream = File.Create(path);

            downloadPath = path;
            showDialog = true;

            return true;
        }
    }
}
